<template>
  <el-card>
    <el-row :gutter="20">
      <el-col :span="7">
        <el-input placeholder="请输入商会名称" v-model="queryForm.name" clearable/>
      </el-col>
      <el-button type="primary" @click="initTableData">查询</el-button>
      <el-button @click="handleModifyDialog(null, 'create')">新增</el-button>
    </el-row>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="ID" width="180" />
      <el-table-column prop="jobTitle" label="职位名称" width="180" />
      <el-table-column prop="jobDesc" label="职位描述" width="180" />
      <el-table-column prop="companyDesc" label="公司描述" width="180" />
      <el-table-column prop="salaryRange" label="薪资范围" width="180" />
      <el-table-column label="操作" width="180">
        <template v-slot="scope">
          <el-button @click="handleModifyDialog(scope.row.id, 'update')">编辑</el-button>
          <el-button @click="handleModifyDialog(scope.row.id, 'view')">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        v-model:current-page="queryForm.pageNum"
        v-model:page-size="queryForm.pageSize"
        :page-sizes="[10, 20, 30, 40, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        prev-text="上一页"
        next-text="下一页"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </el-card>
  <ModifyDialog v-model="modifyDialogVisible" @initTableData="initTableData" :jobId="jobId" :operatorType="operatorType" />
</template>

<script setup>
import {ref} from 'vue'
import axios from '@/util/axios'
import {ElMessage} from 'element-plus'
import ModifyDialog from './modifyDialog'

const jobId = ref(-1)
const modifyDialogVisible = ref(false)
const operatorType = ref('')
const queryForm = ref({
  name: '',
  pageNum: 1,
  pageSize: 10
})
const total = ref(0)
const tableData = ref([])

const initTableData = async () => {
  try {
    queryForm.value.pageNum -= 1
    const result = await axios.get("/job-info/page", queryForm.value)
    const data = result.data
    if (result.status === 200) {
      tableData.value = data.content;
      queryForm.value.pageNum = data.number + 1
      queryForm.value.pageSize = data.size
      total.value = data.totalElements
    }else {
      ElMessage.error("请求失败")
    }
  }catch (e) {
    ElMessage.error("请求报错")
  }
}

initTableData()

const handleModifyDialog = (id, type) => {
  jobId.value = id
  modifyDialogVisible.value = true
  operatorType.value = type
}

const handleSizeChange = (pageSize) => {
  queryForm.value.pageSize = pageSize
  initTableData()
}

const handleCurrentChange = (pageNum) => {
  queryForm.value.pageNum = pageNum
  initTableData()
}

</script>

<style lang="scss" scoped>
.el-table {
  margin: 20px 0;
}
</style>
